<template>
  <!--基本信息  -->
  <div>
    <FormPanel
      ref="formPanel"
      :list="initForm"
      :cols="2"
      :formType="type"
      :labelWidth="110"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button
        v-if="type !== 'show'"
        :loading="saveLoading"
        @click="saveDialog"
        type="primary"
      >{{ btnText }}</el-button>
    </div>
    <!-- 选择车型 -->
    <BaseDialog
      title="选择车型"
      :visible.sync="visibleDialog"
      width="700px"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <infoModal selectType="single" @close="selectModelBack" />
    </BaseDialog>
  </div>
</template>

<script>
import { authority } from '@/utils/authMixin';
import { dictionary } from '@/utils/dictMixin';
import { getTimeStr } from '@/utils/utils';
import { updateFollowInfo } from '@/api/cms-api/customerManage/continueManage/record/index';
import { addFollowInfo } from '@/api/cms-api/customerManage/continueManage/record/index';
import { addApply } from '@/api/cms-api/customerManage/continueManage/record/index';
import infoModal from '@/pages/cms-common/model';
import { getBrand, getSeries, getModelPage, toSelectData } from '@/api/cms-common/basedata';
export default {
  mixins: [authority, dictionary],
  components: {
    infoModal
  },
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '提 交', edit: '修 改' };
    this.saveData = null;
    return {
      followId: this.rowData['followId'],
      projectId: this.rowData['projectId'],
      initForm: this.initFormFun(),
      saveLoading: false,
      visibleDialog: false,
      defeatModel: null,
      formValue: {}
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  mounted() {
    this.loadBrand();
    var myDate = new Date(); //Thu Dec 28 2017 10:</span>07:39 GMT+0800 (中国标准时间)
    let str = getTimeStr(myDate);
    this.initForm.find(item => item.fieldName === 'applyDate').initialValue = str;
  },
  methods: {
    async loadBrand() {
      let b = await getBrand({});
      this.initForm[2].itemList = toSelectData(b.data, 'brandId', 'brandName');
    },
    isChange(val){
        if (val == 25231001) {
          //暂不采购， 隐藏 战败车型 战败品牌字段
            this.initForm[0].initialValue = val;
            this.initForm[2].hidden = true;
            this.initForm[3].hidden = true;
        }else {
            this.initForm[0].initialValue = val;
            this.initForm[2].hidden = false;
            this.initForm[3].hidden = false;
        }
    },
    //车型弹窗关闭
    selectModelBack(data) {
   
      if (data && data.data && typeof data.data != null) {
        this.initForm[3].initialValue = data.data[0].modelName; //回显选择车型名称
        this.defeatModel = data.data[0].modelId; //存储车型ID
      }

      this.visibleDialog = false; //关闭弹窗
    },
    //车型弹窗打开
    openModelDialog() {
      this.visibleDialog = true;
    },
    init() {
     
      if (this.type === 'edit') {
        this.initForm.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    initFormFun() {
      return [
        {
          type: 'SELECT',
          label: '战败类型',
          fieldName: 'defeatType',
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('2523'),
          change: this.isChange,
          rules: [{ required: true, message: '请输入战败类型', trigger: 'blur' }]
        },
        {
          type: 'DATE',
          label: '战败时间:',
          fieldName: 'applyDate',
          valueFormat: 'yyyy-MM-dd HH:mm:ss',
          rules: [{ required: true, message: '请填写战败时间', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '战败品牌',
          fieldName: 'defeatBrandStr',
          placeholder: '请选择',
          initialValue: '',
          hidden:false
          //itemList: []
        },
        {
          type: 'INPUT',
          label: '战败车型:',
          fieldName: 'defeatModelStr',
          placeholder: '请选择',
          hidden:false
          // unitRender: () => {
          //   return <el-button slot="append" icon="el-icon-search" onClick={() => this.openModelDialog()}></el-button>;
          // }
        },
        {
          type: 'INPUT',
          label: '战败原因:',
          fieldName: 'applyReason',
          disabled: false,
          rules: [{ required: true, message: '请填写战败原因', trigger: 'blur' }, { max: 200, message: '战败原因不能超过200', trigger: 'blur' }]
        }
      ];
    },
    async formChangeHandle(val) {
      this.formValue = val;
      if (this.formValue !== null) {
        this.saveLoading = true;
        let res = {};
        var date = "";
        if(this.formValue.applyDate){
          date = getTimeStr(new Date(this.formValue.applyDate));
        }
        this.formValue.applyDate = date ;
        if(this.formValue.defeatType == 25231001) {
              //暂不采购
        // this.formValue = Object.assign({}, { defeatModel: '' }, this.formValue);
        // this.formValue = Object.assign({}, { defeatBrand: '' }, this.formValue);
        //  this.formValue = Object.assign({}, { modelName: '' }, this.formValue);
         this.formValue = Object.assign({}, { defeatModelStr: '' }, this.formValue);
         this.formValue = Object.assign({}, { defeatBrandStr: '' }, this.formValue);
        }else {
            //购买其他品牌
            //this.formValue = Object.assign({}, { defeatModel: this.defeatModel }, this.formValue);
      
        }


       this.formValue = Object.assign({}, { projectId: this.projectId }, this.formValue);
        this.formValue = Object.assign({}, { applyType: '25221001' }, this.formValue);
        this.formValue = Object.assign({}, { applyStatus: '25241001' }, this.formValue);
        if (this.type === 'add') {
          res = await addApply(this.formValue);
        }
        if (this.type === 'edit') {
          res = await updateFollowInfo(this.formValue, this.followId);
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }else {
          this.$notify.warning({title:'提示信息',message:res.errMsg })
          this.closeDialog('refresh');
        }

      }
    },
    saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
        formatDates: function(value) {
      // 这里的 value 就是需要过滤的数据
      if (value == null || value == '') {
        return '';
      }
      
      var date = new Date(value);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      if (month < 10) {
        month = "0"+month;
      }
      var day = date.getDate();
      if (day < 10) {
        day = "0"+day;
      }
      return year + '-' + month + '-' + day + ' 00:00:00';
    }
  }
};
</script>
<style scoped>
.wrapper {
  height: calc(100vh - 110px);
}
</style>
